--==============================
--[[ 猪猪晗buff相关函数 ]]
--==============================
local function OnAttack_piggycorolla(attacker, data)
    local fx = SpawnPrefab("sw_buff_piggycorolla_fx")
    if fx ~= nil then
        fx.Transform:SetPosition(data.target.Transform:GetWorldPosition())
    end
end

--==============================
--[[ 追光逐影buff相关函数 ]]
--==============================
local spdamage = {
    planar = 100,
}
local function chasing_light_shadow(owner, data)
    local target = data.target
    target.components.combat:GetAttacked(data.weapon or target, 0, nil, nil, spdamage)
end

--==============================
--[[ 光影交织塔buff相关函数 ]]
--==============================
local function OnTick(inst, target)
    if target.components.health ~= nil and
        not target.components.health:IsDead() and
        not target:HasTag("playerghost") then
        target.components.health:DoDelta(3, nil, "sw_buff_shadowwoven_tower")
    else
        inst.components.debuff:Stop()
    end
end

--==============================
--[[ buff列表 ]]
--==============================
local buffs = {
    -- 猪猪晗
    piggycorolla = {
        duration = TUNING.TOTAL_DAY_TIME,     -- buff时长
        priority = 1,                         -- 优先级
        onattachedfn = function(inst, target) -- buff触发的执行函数
            if target.components.combat ~= nil then
                target.components.combat.externaldamagemultipliers:SetModifier(inst, 1.25)
            end
            target:ListenForEvent("onattackother", OnAttack_piggycorolla)
        end,
        onextendedfn = function(inst, target) -- buff存在期间，添加了相同buff的执行函数
        end,
        ondetachedfn = function(inst, target) -- buff结束时的执行函数
            if target.components.combat ~= nil then
                target.components.combat.externaldamagemultipliers:RemoveModifier(inst)
            end
            target:RemoveEventCallback("onattackother", OnAttack_piggycorolla)
        end,
    },
    -- 夜莓小蛋糕
    night_berry_cake = {
        duration = 2 * TUNING.TOTAL_DAY_TIME,
        priority = 1,
        overlay = 2400, -- 可叠加,最长buff时间为40min
        onattachedfn = function(inst, target)
            if target.swnightvision then
                target.swnightvision:set(true)
            end
        end,
        onextendedfn = function(inst, target)
        end,
        ondetachedfn = function(inst, target)
            if target.swnightvision then
                target.swnightvision:set(false)
            end
        end,
    },
    -- 追光逐影
    chasing_light_shadow = {
        duration = TUNING.TOTAL_DAY_TIME,
        priority = 1,
        onattachedfn = function(inst, target)
            if target.components.combat ~= nil then
                target:ListenForEvent("onhitother", chasing_light_shadow)
            end
        end,
        onextendedfn = function(inst, target)
        end,
        ondetachedfn = function(inst, target)
            if target.components.combat ~= nil then
                target:RemoveEventCallback("onhitother", chasing_light_shadow)
            end
        end,
    },
    -- 光影交织塔
    shadowwoven_tower = {
        duration = 15,
        priority = 1,
        onattachedfn = function(inst, target)
            inst.task = inst:DoPeriodicTask(5, OnTick, nil, target)
        end,
        onextendedfn = function(inst, target)
        end,
        ondetachedfn = function(inst, target)
            inst:Remove()
        end,
    },
}

return buffs
